//
//  ItemForGuoNei.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ItemForGuoNei.h"
#import "CellForGuoNei.h"
#import "PCH.pch"
#import "ModelForGuoNei.h"
#import <UIImageView+WebCache.h>


@interface ItemForGuoNei () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation ItemForGuoNei

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_arrayOfModel release];
    [_layout release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {

    self.arrayOfModel = [NSMutableArray array];
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.minimumInteritemSpacing = 20;
    self.layout.minimumLineSpacing = 20;
    self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 60) / 2.0, (self.contentView.frame.size.width - 60) / 2 * 1.15);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
    [self.contentView addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    
    [self.collectionView registerClass:[CellForGuoNei class] forCellWithReuseIdentifier:@"pool"];

#pragma mark - 解析数据
    [NetworkingHandler networkingWithURL:@"http://chanyouji.com/api/destinations.json?page=1" completeHandle:^(NSData *data,id result) {
        
        NSArray *array = [[result objectAtIndex:3] objectForKey:@"destinations"];
        for (NSDictionary *dic in array) {
            
            ModelForGuoNei *model = [[ModelForGuoNei alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayOfModel addObject:model];
            [model release];
            
        }
        NSArray *array1 = [[result objectAtIndex:4] objectForKey:@"destinations"];
        for (NSDictionary *dic in array1) {
            
            ModelForGuoNei *model = [[ModelForGuoNei alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrayOfModel addObject:model];
            [model release];
            
        }
        
        [self.collectionView reloadData];
        
        
    }];
    
    }

    return self;
}

#pragma mark - collectionView协议方法
/** 返回item个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayOfModel.count;
}

/** 返回cell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForGuoNei *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    
    [cell passModel:[self.arrayOfModel objectAtIndex:indexPath.item]];
    
    return cell;
    
}

#pragma mark - 布局子视图
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.collectionView.frame = self.contentView.frame;
    
}


#pragma mark - 选中item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelForGuoNei *model = [self.arrayOfModel objectAtIndex:indexPath.item];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passID" object:nil userInfo:@{@"ID" : model.id, @"name" : model.name_zh_cn}];
    
}







@end
